/**
 * Free software.
 */

package org.nanhill.commons.model.tree;

import java.util.LinkedList;
import java.util.List;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2007-4-15
 */
public class DefaultCategory extends AbstractTreeNode<Category> implements WritableCategory {
	private static final long serialVersionUID = 8489178404442327171L;

	private long id;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPathname() {
		final StringBuilder builder = new StringBuilder(name);
		for (Category tmpParent = getParent(); tmpParent != null;) {
			builder.insert(0, " > ");
			String catName = tmpParent.getName();
			builder.insert(0, catName);
			tmpParent = tmpParent.getParent();
		}
		return builder.toString();
	}

	public List<Category> getPath() {
		// here linked list is better
		final List<Category> path = new LinkedList<Category>();
		path.add(this);
		for (Category tmpParent = getParent(); tmpParent != null;) {
			path.add(0, tmpParent);
			tmpParent = tmpParent.getParent();
		}
		return path;
	}

	public int getFloor() {
		int f = 1;
		for (Category tmpParent = getParent(); tmpParent != null; f++)
			tmpParent = tmpParent.getParent();
		return f;
	}
}
